package com.example.demoasync.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

/**
 * 类${CLASS_NAME}的实现描述：
 *
 * @Author liyanjun 2018-12-27 14:36
 */
@Component
public class AsyncComponent {

    @Async
    public void asyncMethodWithVoidReturnType(boolean isValid) {
        if (isValid) {
            System.out.println("Excute method synchronously." + Thread.currentThread().getName());
        } else {
            throw new IllegalStateException("Invalid");
        }
    }

    @Async
    public Future<String> asyncMethodWithReturnType() {
        System.out.println("Execute method asynchronously - "
                + Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
            return new AsyncResult<String>("hello world !!!!");
        } catch (InterruptedException e) {
            //
        }

        return null;
    }

    @Async("threadPoolTaskExecutor")
    public void asyncMethodWithConfiguredExecutor() {
        System.out.println("Execute method with configured executor - "
                + Thread.currentThread().getName());
    }
}
